package org.opsli.modulars.business.dailyReport.web;


import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.opsli.api.base.result.ResultVo;
import org.opsli.common.annotation.ApiRestController;
import org.opsli.core.persistence.Page;
import org.opsli.core.utils.UserUtil;
import org.opsli.modulars.business.dailyReport.entity.DailyReport;
import org.opsli.modulars.business.dailyReport.service.impl.DailyReportServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collections;
import java.util.List;

/**
 * @author zhangyifan
 */
@Slf4j
@ApiRestController("/dailyReport")
public class DailyReportController {
    @Autowired
    private DailyReportServiceImpl service;

    /**
     * 获取待办任务，已完成任务，未完成任务的日报表的controller方法
     * @param pageNo
     * @param pageSize
     * @param dailyReport
     * @return
     */
    @RequestMapping(value = "/getDailyReport")
    public ResultVo<?> getDailyReport(Integer pageNo, Integer pageSize,DailyReport dailyReport) {
        dailyReport.setDevid(UserUtil.getSysUser().getCompanyId());
        List<DailyReport> list = service.getDailyReport(dailyReport);
        Collections.reverse(list);

        //将合并的list进行分页
        Page page = new Page(pageNo, pageSize);
        int totalLength = list.size(); //总的数据条数
        int totalPage = totalLength % pageSize == 0 ? totalLength / pageSize : totalLength / pageSize + 1; //总的页数
        List pageData = null;
        if (pageNo < totalPage) {
            int startIndex = (pageNo - 1) * pageSize;
            int endIndex = startIndex + pageSize - 1;
            pageData = list.subList(startIndex, endIndex + 1);
        } else {
            int startIndex = (pageNo - 1) * pageSize;
            pageData = list.subList(startIndex, totalLength);//最后一页
        }
        page.setList(pageData);
        page.setTotal(list.size());
        return ResultVo.success(page.getPageData());
    }
}
